﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Bank_database_system
{
    public partial class 工资发放a : UserControl
    {
        int NowPage = 1;//当前页码
        int PageNumber = 1;//页码总数
        List<string> staff_ID_list = new List<string>();//记录被选中的员工ID列表

        //创建勾选框列
        DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn
        {
            Name = "CheckBoxColumn",  //列名
            HeaderText = "选择",      //列标题
            Width = 50,               //列宽度
            ReadOnly = false          //是否只读，设置为 false 使勾选框可编辑
        };

        public 工资发放a()
        {
            InitializeComponent();
            textBox2.Text = AccountCheck.branchname;
            dataGridView1.DataSource = Finance.Select_Staff(string.Empty, string.Empty, string.Empty, AccountCheck.branchid, NowPage, 财务首页.PageSize);
            //订阅 CellContentClick 事件
            dataGridView1.CellContentClick += DataGridView1_CellContentClick;
            Controls.Add(dataGridView1);
            dataGridView1.Columns.Add(checkBoxColumn);//在列表最右侧添加一个勾选框列
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false; // 禁用添加空白行
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns["CheckBoxColumn"].Width = 50;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
            PageNumber = Finance.GetStaffPageNumber(string.Empty, string.Empty, string.Empty, AccountCheck.branchid);
            label3.Text = PageNumber.ToString();
            label1.Text = NowPage.ToString();
            staff_ID_list.Clear();
            if (PageNumber == 0)
            {
                NowPage = 0;
            }
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //确保点击的是勾选框列
            if (e.ColumnIndex == dataGridView1.Columns["CheckBoxColumn"].Index && e.RowIndex >= 0)
            {
                //检查勾选框是否被选中
                bool isChecked = Equals(dataGridView1.Rows[e.RowIndex].Cells["CheckBoxColumn"].Value, true);

                //使用 isChecked 变量来判断勾选框是否被选中
                if (isChecked)
                {
                    //如果勾选框被选中
                    dataGridView1.Rows[e.RowIndex].Cells["CheckBoxColumn"].Value = false;
                    if (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() != null)
                    {
                        if (!staff_ID_list.Remove(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()))
                        {
                            MessageBox.Show($"移除员工 {dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()} 失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("移除员工ID异常！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    //如果勾选框未被选中
                    dataGridView1.Rows[e.RowIndex].Cells["CheckBoxColumn"].Value = true;
                    if (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() != null)
                    {
                        staff_ID_list.Add(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
                    }
                    else
                    {
                        MessageBox.Show("添加员工ID异常！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NowPage = 1;
            textBox1.Clear();
            textBox5.Clear();
            comboBox1.Text = null;
            dataGridView1.DataSource = Finance.Select_Staff(string.Empty, string.Empty, string.Empty, AccountCheck.branchid, NowPage, 财务首页.PageSize);
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            PageNumber = Finance.GetStaffPageNumber(string.Empty, string.Empty, string.Empty, AccountCheck.branchid);
            label3.Text = PageNumber.ToString();
            label1.Text = NowPage.ToString();
            staff_ID_list.Clear();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            NowPage = 1;
            DataTable dataTable = Finance.Select_Staff(textBox5.Text, textBox1.Text, comboBox1.Text, AccountCheck.branchid, NowPage, 财务首页.PageSize);
            if (dataTable.Rows.Count > 0)
            {
                dataGridView1.DataSource = dataTable;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                PageNumber = Finance.GetStaffPageNumber(textBox5.Text, textBox1.Text, comboBox1.Text, AccountCheck.branchid);
                label3.Text = PageNumber.ToString();
                label1.Text = NowPage.ToString();
            }
            staff_ID_list.Clear();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            PageNumber = Finance.GetStaffPageNumber(textBox5.Text, textBox1.Text, comboBox1.Text, AccountCheck.branchid);
            label3.Text = PageNumber.ToString();
            NowPage -= 1;
            if (NowPage <=0)
            {
                NowPage = 1;
                if (PageNumber == 0)
                {
                    NowPage = 0;
                }
            }
            DataTable dataTable = Finance.Select_Staff(textBox5.Text, textBox1.Text, comboBox1.Text, AccountCheck.branchid, NowPage, 财务首页.PageSize);
            if (dataTable.Rows.Count > 0 || PageNumber == 0)
            {
                dataGridView1.DataSource = dataTable;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                label1.Text = NowPage.ToString();
            }
            staff_ID_list.Clear();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            PageNumber = Finance.GetStaffPageNumber(textBox5.Text, textBox1.Text, comboBox1.Text, AccountCheck.branchid);
            label3.Text = PageNumber.ToString();
            NowPage += 1;
            if (NowPage > PageNumber)
            {
                NowPage = PageNumber;
            }
            DataTable dataTable = Finance.Select_Staff(textBox5.Text, textBox1.Text, comboBox1.Text, AccountCheck.branchid, NowPage, 财务首页.PageSize);
            if (dataTable.Rows.Count > 0 || PageNumber == 0)
            {
                dataGridView1.DataSource = dataTable;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                label1.Text = NowPage.ToString();
            }
            staff_ID_list.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Finance.Pay_Wages(staff_ID_list, AccountCheck.branchid, AccountCheck.staffid);
            DataTable dataTable = Finance.Select_Staff(textBox5.Text, textBox1.Text, comboBox1.Text, AccountCheck.branchid, NowPage, 财务首页.PageSize);
            if (dataTable.Rows.Count > 0)
            {
                dataGridView1.DataSource = dataTable;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            staff_ID_list.Clear();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            staff_ID_list.Clear();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                //确保不处理新行
                if (!row.IsNewRow)
                {
                    string state = row.Cells[6].Value.ToString(); //获取工资发放情况
                    if(state == "未发放")
                    {
                        //检查勾选框是否被选中
                        bool isChecked = Equals(row.Cells["CheckBoxColumn"].Value, true);

                        //使用 isChecked 变量来判断勾选框是否被选中
                        if (isChecked)
                        {
                            //如果勾选框已经被选中
                            if (row.Cells[1].Value.ToString() != null)
                            {
                                staff_ID_list.Add(row.Cells[1].Value.ToString());
                            }
                            else
                            {
                                MessageBox.Show("添加员工ID异常！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                        else
                        {
                            //如果勾选框未被选中
                            row.Cells["CheckBoxColumn"].Value = true;
                            if (row.Cells[1].Value.ToString() != null)
                            {
                                staff_ID_list.Add(row.Cells[1].Value.ToString());
                            }
                            else
                            {
                                MessageBox.Show("添加员工ID异常！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                }
            }
        }
    }
}
